﻿namespace Collaboris.Saf.Converters.Spring
{
    using System;
    using System.Web;

    using Definitions;

    using Utils;

    public abstract class SafConverter<T>
        where T : class
    {
        public virtual string Convert(T source, bool renderAsTemplate, string propertyName)
        {
            return null;
        }

        public virtual string Convert(SafPropertyDefinition classDefinition, bool renderAsTemplate)
        {
            // the Underlying Type needs to be the same!
            if (classDefinition.UnderlyingType != typeof(T))
                throw new ArgumentException("The class definition's 'UnderlyingType' doesn't match '" + typeof(T).GetType().ToString() + "'!");

            return string.Empty;
        }

        protected bool IsNotXml(string example)
        {
            if (string.IsNullOrEmpty(example))
                return true;

            if (example.StartsWith("<") && example.EndsWith(">"))
                return false;

            return true;
        }
        
        public virtual string ConvertToXmlElement(SafPropertyDefinition propertyDefinition)
        {
            string elementXml = string.Empty;


            if (!string.IsNullOrEmpty(propertyDefinition.SerializedExample))
            {
                elementXml += @"<" + propertyDefinition.ParentedName.ToLower() + @">" + propertyDefinition.SerializedExample + @"</" +
                              propertyDefinition.ParentedName.ToLower() + @">";
            }
            else
            {
                elementXml += @"<" + propertyDefinition.ParentedName.ToLower() + @">" + propertyDefinition.Example + @"</" +
                              propertyDefinition.ParentedName.ToLower() + @">";
            }

            return elementXml;
        }

        public virtual string ConvertToKeyValuePair(SafPropertyDefinition propertyDefinition)
        {
            string elementXml = string.Empty;

            string example;
            if (!string.IsNullOrEmpty(propertyDefinition.SerializedExample))
            {
                example = HttpUtility.HtmlAttributeEncode(propertyDefinition.SerializedExample);
                //example = propertyDefinition.SerializedExample.Replace("<", "&lt;");
            }
            else
            {
                if (IsNotXml(propertyDefinition.Example))
                    example = propertyDefinition.Example;
                else
                    example = HttpUtility.HtmlAttributeEncode(propertyDefinition.Example);
                    //example = propertyDefinition.Example.Replace("<", "&lt;");
            }

            elementXml += @"<Property Key=""" + propertyDefinition.ParentedName.ToLower() + @""" Value=""" + example + @"""/>";

            return elementXml;
        }

        public virtual string ConvertToCommandLineArg(SafPropertyDefinition propertyDefinition)
        {

            string commandLineArg = string.Empty;

            string example;
            if (!string.IsNullOrEmpty(propertyDefinition.SerializedExample))
            {
                example = HttpUtility.HtmlAttributeEncode(propertyDefinition.SerializedExample);
                //example = propertyDefinition.SerializedExample.Replace("<", "&lt;");
            }
            else
            {
                if (IsNotXml(propertyDefinition.Example))
                    example = propertyDefinition.Example;
                else
                    example = HttpUtility.HtmlAttributeEncode(propertyDefinition.Example);
                //example = propertyDefinition.Example.Replace("<", "&lt;");
            }

            commandLineArg += @"-" + propertyDefinition.ParentedName.ToLower() + @" """ + example + @""" ";

            return commandLineArg;
        }


        
    }
}
